package com.example.mediamoudle.multivideo;


import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.mediamoudle.crop.VideoInfo;

import java.io.IOException;
import java.util.List;

public class VideoPreviewView extends SurfaceView implements  MediaPlayerWrapper.IMediaCallback,SurfaceHolder.Callback {
    private MediaPlayerWrapper mMediaPlayer;

    /**视频播放状态的回调*/
    private MediaPlayerWrapper.IMediaCallback callback;

    public VideoPreviewView(Context context) {
        this(context,null);
        init(context);
    }

    public VideoPreviewView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        init(context);
    }

    public VideoPreviewView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mMediaPlayer = new MediaPlayerWrapper();
        mMediaPlayer.setOnCompletionListener(this);
        getHolder().addCallback(this);
    }

    /**设置视频的播放地址*/
    public void setVideoPath(List<String> paths){
        mMediaPlayer.setDataSource(paths);
    }


    /**是否循环播放*/
    public void setLooping(boolean isLooping) {
        mMediaPlayer.setLooping(isLooping);
    }

    public void play(){
        mMediaPlayer.play();
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i("TAG", "surfaceCreated: ");
        Surface surface = holder.getSurface();
        mMediaPlayer.setSurface(surface);
        try {
            mMediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //   mMediaPlayer.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void onDestroy(){
        if (mMediaPlayer != null) {
            if(mMediaPlayer.isPlaying()){
                mMediaPlayer.stop();
            }
            mMediaPlayer.release();
        }
    }



    @Override
    public void onVideoPrepare() {
        if (callback!= null){
            callback.onVideoPrepare();
        }
    }

    @Override
    public void onVideoStart() {
        if(callback!=null){
            callback.onVideoStart();
        }
    }

    public int getVideoWidth(){
        return mMediaPlayer.getVideoWidth();
    }

    public int getVideoHeight(){
        return mMediaPlayer.getVideoHeight();
    }


    @Override
    public void onVideoPause() {
        if (callback != null){
            callback.onVideoPause();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (callback != null){
            callback.onCompletion(mp);
        }
    }


    @Override
    public void onVideoChanged(final VideoInfo info) {
        if(callback!=null){
            callback.onVideoChanged(info);
        }
    }
    /**
     * isPlaying now
     * */
    public boolean isPlaying(){
        return mMediaPlayer.isPlaying();
    }
    /**
     * pause play
     * */
    public void pause(){
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
        }
    }

    /**
     * start play video
     * */
    public void setVolume(float leftVolume, float rightVolume){
        mMediaPlayer.setVolume(leftVolume);
    }

    /**
     * start play video
     * */
    public void start(){
        mMediaPlayer.start();
    }



    /**
     * 跳转到指定的时间点
     * */
    public void seekTo(int time) {
        mMediaPlayer.seekTo(time);
    }

    /**
     * 获取视频的长度
     * */
    public int getVideoDuration(){
        return mMediaPlayer.getVideoDuration();
    }



    /**
     * 获取当前视频的长度
     * */
    public int getCurPosition(){
        return mMediaPlayer.getCurPosition();
    }

    public void setIMediaCallback(MediaPlayerWrapper.IMediaCallback callback){
        this.callback=callback;
    }



}


